package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.NetInfoDao;
import com.bestv.search.common.model.NetInfo;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NetInfoDaoHibernate extends GenericDaoHibernate<NetInfo, Long> implements NetInfoDao {

    @Override
    public NetInfo getInfoByNetId(String netId) {
        String hql = "from NetInfo where netId = '" + netId + "'";
        List<?> list = getHibernateTemplate().find(hql);
        return list == null || list.size() == 0 ? null : (NetInfo) list.get(0);
    }

    @Override
    public NetInfo getInfoByInfoId(long info_id) {
        String sql = "select genre,area,released,performers,directors from info_netInfo inner join"
                + " netInfo on info_netInfo.netInfo_id =netInfo.id where  info_netInfo.info_id=? "
                + " order by released desc";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql).addScalar("genre", Hibernate.STRING)
                    .addScalar("area", Hibernate.STRING).addScalar("released", Hibernate.STRING)
                    .addScalar("performers", Hibernate.STRING).addScalar("directors", Hibernate.STRING)
                    .setResultTransformer(Transformers.aliasToBean(NetInfo.class));
            query.setLong(0, info_id);

            @SuppressWarnings("unchecked")
            List<NetInfo> list = query.list();

            return list == null || list.size() == 0 ? null : list.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (session != null)
                releaseSession(session);
        }
    }

    @Override
    public void saveReference(long info_id, long netInfo_id) {
        String sql = "insert into info_netInfo(netInfo_id, info_id) values(?, ?)";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql);
            query.setLong(0, netInfo_id);
            query.setLong(1, info_id);
            query.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
    }

    @Override
    public boolean existReference(long info_id, long netInfo_id) {
        String sql = "select * from info_netInfo where netInfo_id = ? and info_id = ?";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql);
            query.setLong(0, netInfo_id);
            query.setLong(1, info_id);
            List<?> list = query.list();
            return list == null || list.size() == 0 ? false : true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
        return false;
    }

    @Override
    public List<String> getAllName(int start, int limit) {
        String sql = "select distinct name from netInfo";
        Session session = null;
        List<String> result = new ArrayList<String>();
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            query.setFirstResult(start);
            query.setMaxResults(limit);
            List<?> list = query.list();
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    @SuppressWarnings("rawtypes")
                    Map map = (Map) list.get(i);
                    String name = (String) map.get("name");
                    result.add(name);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }

        return result;
    }

    @Override
    public int getNameCount() {
        String sql = "select count(distinct name) as amount from netInfo";
        Session session = null;
        int count = 0;
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql);
            List<?> list = query.list();
            if (list != null && list.size() > 0) {
                if (list.get(0) != null) {
                    count = Integer.valueOf(list.get(0).toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
        return count;
    }
}